import $ from '../common';
Page({
  data: {
    titleHeight: $.gData().titleHeight,
    statuHeight: $.gData().statuHeight,
  },
  textinputhand(e) {
    this.setData({
      content: e.detail.value
    })
  },
  afterRead(event) { //上传
    const {
      file
    } = event.detail;
    const {
      imgs
    } = this.data
    let files = file.map(v => { //因为vant 返回的解构是[{url:'............'}] $.uploadFileList 需要的结构式[1,2,3]
      return v.url
    })
    $.uploadFileList("/image/upload/oss", {
      prefix: "keday/",
      fileList: files
    }, res => {
      let a = res.map(i => { //返回的结构式[1,2,3,5] 需要改成vant需要的[{url:'............'},{}]
        return {
          url: i
        }
      })
      let arr //判断之前有没有 图片 有的话合并
      if (imgs) {
        arr = [...imgs, ...a]
      } else {
        arr = [...a]
      }
      this.setData({
        'imgs': arr
      })
    })
  },
  imgdelete(event) { //删除图片
    const {
      imgs
    } = this.data
    imgs.splice(event.detail.index, 1)
    this.setData({
      imgs
    })
  },
  save() {
    let {content,imgs} = this.data
    if (!content) {
      $.tip('评价内容不能为空')
      return
    }
    if (imgs.length > 0) {
      let a = imgs.map((v, i) => { //保存的时候需要将 [{url:'............'},{}] 改成[1,2,3]
        return imgs[i] = v.url
      });
      imgs = a
    }
    $.parent().setData({
      'textvalue': content,
      'imglist': imgs || []
    })
    wx.navigateBack({
      delta: -1,
    })
  },
  onLoad(e) {
    let content = $.parent().data.textvalue || '';
    let imgs = $.parent().data.imglist || []
    if (imgs && imgs.length > 0) {
      let a = imgs.map(i => {
        return {
          url: i
        }
      })
      imgs = a
    }
    this.setData({
      content,
      imgs
    })
  },
  onShow() {},
})